﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Impls;
using Microhard.WeixinMP.Interfaces;
using Micrhard.FMCG.Server.Msg;
using Microhard.WeixinMP.Models;
using Microhard.Permission.Interface;
using Microhard.Core;
using Micrhard.FMCG.Server.Interface;
using System.Configuration;
using Microhard.WeixinMP.Unities;

namespace Micrhard.FMCG.Server.Weixin
{
    public class GetNotice : BaseResponseImpl
    {
        public override Microhard.WeixinMP.Interfaces.WeixinCommand Command
        {
            get
            {
                WeixinCommand wc = new WeixinCommand();
                wc.Event = "CLICK";
                wc.EventKey = "myMsg";
                return wc;
            }
        }

        protected override Microhard.WeixinMP.Models.WeixinBaseData<object> CreateFeatureResponse(Microhard.WeixinMP.Models.RequestServiceModel request, Microhard.WeixinMP.Models.WeixinBaseData<object> response)
        {
            //创建token
            string token = SessionSecurity.CreateToken(response.ToUserName);
            response.MsgType = WeixinMsgType.Text;
            response.FuncFlag = WeixinMsgType.FuncFlag_False;
            WeixinTxt result = new WeixinTxt();

            //验证用户是否合法
            IWeiXinAccount iwa = IoC.Resolve<IWeiXinAccount>();
            string realUserName = string.Empty;
            bool illegal = iwa.ValidateByWeiXinUserName(response.ToUserName, out realUserName);
            if (illegal)
            {
                //生成新闻
                INotice notice = IoC.Resolve<INotice>();
                var ns = notice.GetNoticesByWeixinOpenID(response.ToUserName);
                //拼装消息
                if (ns == null || ns.Count == 0)
                {
                    result.Content = NoticeMsg.NoneNotice;
                    response.Content = result;
                }
                else
                {
                    response.MsgType = WeixinMsgType.News;
                    WeixinNews_Response news = new WeixinNews_Response();
                    news.Items = new List<WeixinNewsItem_Response>();
                    ns.Take(10).ToList().ForEach(n =>
                    {
                        string finUrl = string.Format(this.UrlFormat, ConfigurationManager.AppSettings["NewsDetailUrl"], token, response.EncodeWeixinToUserName, string.Empty, request.ConversationID) + "&noticeID=" + n.NoticeID;
                        news.Items.Add(new WeixinNewsItem_Response
                        {
                            Title = n.Title,
                            Description = n.Message,
                            Url = finUrl
                        });
                    });
                    response.Content = news;
                }
            }
            else
            {
                result.Content = NoticeMsg.NoneUser;
                response.Content = result;
            }
            return response;
        }
    }
}
